package cn.yjr.vector.tools;

import org.apache.http.client.CookieStore;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.protocol.HttpContext;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.BinaryHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;

/**
 * @author vector
 *
 */
public class YJRHttpClient {
	public static JSONObject userData;
	public static JSONObject initData;
	private static final String BASE_URL = "http://115.28.222.1/yijiaren";
	private static AsyncHttpClient client = new AsyncHttpClient();  
	static{
		client.addHeader("yijiaren", "android");
	} 
	public static void get(Context context,String url, BinaryHttpResponseHandler responseHandler) {  
		setCookie(context);
		client.get(getAbsoluteUrl(url),responseHandler);  
	}  
	public static void post(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
		setCookie(context);
		client.post(getAbsoluteUrl(url), params, responseHandler);  
	}
	private static String getAbsoluteUrl(String relativeUrl) {  
		return BASE_URL + relativeUrl;  
	}
	public static void setCookie(Context context){
		PersistentCookieStore myCookieStore = new PersistentCookieStore(context);  
		client.setCookieStore(myCookieStore); 
	}
	public static void logCookie(Context context){
		SharedPreferences sp = context.getSharedPreferences("CookiePrefsFile", 0);
		String cookie = sp.getString("cookie_JSESSIONID115.28.222.1", "null");
		Log.v("cookie",cookie);
	}
	
	public static void getCookie(){
		HttpContext httpContext = client.getHttpContext();
		CookieStore cookies = (CookieStore) httpContext.getAttribute(ClientContext.COOKIE_STORE);
		if(cookies!=null){
            for(Cookie c:cookies.getCookies()){
                Log.v("cookie","main before ~~"+c.getName()+" \n"+c.getValue());
            }
        }else{
        	 Log.v("cookie","main  before~~ cookies is null");
        }
	}
	
	public static void getInitData(final Context context, String version){
		RequestParams params = new RequestParams(); 
		params.add("version", version);
		YJRHttpClient.post(context,"/data/initData.action", params, new JsonHttpResponseHandler(){
			 @Override  
	            public void onSuccess(JSONObject data) {  
	             	try {
						if(data.getInt("result") == 1){
							YJRHttpClient.setCookie(context); 
							Toast.makeText(context, "获取应用初始化数据成功", Toast.LENGTH_SHORT).show();
							//当前版本为最新
							if(data.getString("info").equals("uptodated")){
								//当前初始化信息为最新，可以直接使用
							}else{
								//当前版本信息已过期
								initData = data;
								//TODO 需要将一些信息存储在本地
							}
								
						}else{
							Toast.makeText(context, data.getString("info"), Toast.LENGTH_SHORT).show();
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	            }  
		});
	}
	
	
	public static void getUserData(final Context context){
		RequestParams params = new RequestParams(); 
		YJRHttpClient.post(context,"/user/refreshUser.action", params, new JsonHttpResponseHandler(){
			 @Override  
	            public void onSuccess(JSONObject data) {  
	             	try {
						if(data.getInt("result") == 1){
							YJRHttpClient.setCookie(context); 
							Toast.makeText(context, "获取用户信息成功", Toast.LENGTH_SHORT).show();
							userData = data;
							//TODO 将用户信息存储在本地
								
						}else{
							Toast.makeText(context, data.getString("info"), Toast.LENGTH_SHORT).show();
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	            }  
		});
	}
}
